package cn.iocoder.yudao.module.enterprise.controller.admin.model;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.enterprise.controller.admin.model.vo.*;
import cn.iocoder.yudao.module.enterprise.convert.model.ModelConvert;
import cn.iocoder.yudao.module.enterprise.dal.dataobject.model.ModelDO;
import cn.iocoder.yudao.module.enterprise.service.model.ModelEmissionService;
import cn.iocoder.yudao.module.enterprise.service.model.ModelService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name =  "管理后台 - 核算模型")
@RestController
@RequestMapping("/enterprise/model")
@Validated
public class ModelController {

    @Resource
    private ModelService modelService;
    @Resource
    private ModelEmissionService modelEmissionService;

    @PostMapping("/create")
    @Operation(summary = "创建核算模型")
    @PreAuthorize("@ss.hasPermission('enterprise:model:create')")
    public CommonResult<Long> createModel(@Valid @RequestBody ModelCreateReqVO createReqVO) {
        return success(modelService.createModel(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新核算模型")
    @PreAuthorize("@ss.hasPermission('enterprise:model:update')")
    public CommonResult<Boolean> updateModel(@Valid @RequestBody ModelUpdateReqVO updateReqVO) {
        modelService.updateModel(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除核算模型")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('enterprise:model:delete')")
    public CommonResult<Boolean> deleteModel(@RequestParam("id") Long id) {
        modelService.deleteModel(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得核算模型")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('enterprise:model:query')")
    public CommonResult<ModelRespVO> getModel(@RequestParam("id") Long id) {
        ModelDO model = modelService.getModel(id);
        return success(ModelConvert.INSTANCE.convert(model));
    }

    @GetMapping("/page")
    @Operation(summary = "获得核算模型分页")
    @PreAuthorize("@ss.hasPermission('enterprise:model:query')")
    public CommonResult<PageResult<ModelRespVO>> getModelPage(@Valid ModelPageReqVO pageVO) {
        PageResult<ModelDO> pageResult = modelService.getModelPage(pageVO);
        return success(ModelConvert.INSTANCE.convertPage(pageResult));
    }

    @PostMapping("/create-emission")
    @Operation(summary = "创建模型的排放源")
    @PreAuthorize("@ss.hasPermission('enterprise:model:create')")
    public CommonResult<Long> createModelEmission(@Valid @RequestBody ModelEmissionCreateReqVO createReqVO) {
        return success(modelEmissionService.createModelEmission(createReqVO));
    }

    @DeleteMapping("/delete-emission")
    @Operation(summary = "删除模型的排放源")
    @Parameter(name = "id", description = "模型和排放源关联ID", required = true)
    @PreAuthorize("@ss.hasPermission('enterprise:model:delete')")
    public CommonResult<Boolean> deleteModelEmission(@RequestParam("id") Long id) {
        modelEmissionService.deleteModelEmission(id);
        return success(true);
    }

    @GetMapping("/page-emission")
    @Operation(summary = "获得模型的排放源分页")
    @PreAuthorize("@ss.hasPermission('enterprise:model:query')")
    public CommonResult<PageResult<ModelEmissionRespVO>> getModelEmissionPage(@Valid ModelEmissionPageReqVO pageVO) {
        return success(modelService.getModelEmissionPage(pageVO));
    }

}
